import subprocess
import logging

# Configure logging
logging.basicConfig(filename='system_rollback.log', level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')

def log_message(message):
    """Log a message to both console and log file."""
    print(message)
    logging.info(message)

def run_command(command):
    """Run a shell command and handle errors."""
    try:
        result = subprocess.run(command, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output = result.stdout.decode('utf-8')
        log_message(output)
    except subprocess.CalledProcessError as e:
        error_message = f"Error executing command: {e.cmd}\n{e.stderr.decode('utf-8')}"
        log_message(error_message)
        exit(1)

def rollback_system():
    """Rollback the system to the previous deployment."""
    log_message("Rolling back the system to the previous deployment...")
    run_command("sudo ostree admin rollback")

def main():
    # Perform system rollback
    rollback_system()

if __name__ == "__main__":
    main()

